<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>对 PUT 方法的支持</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="features.file-upload.multiple.html">« 上传多个文件</a></li>
      <li style="float: right;"><a href="features.file-upload.errors.seealso.html">参见 »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="features.file-upload.html">文件上传处理</a></li>
    <li>对 PUT 方法的支持</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="features.file-upload.put-method" class="sect1">
  <h2 class="title">对 PUT 方法的支持</h2>
  <p class="para">
   PHP 对部分客户端具备的
   HTTP PUT 方法提供了支持。PUT 请求比文件上传要简单的多，它们一般的形式为：
   <div class="informalexample">
    <div class="example-contents">
<div class="HTTPcode"><pre class="HTTPcode">PUT /path/filename.html HTTP/1.1</pre>
</div>
    </div>

   </div>
  </p>
  <p class="para">
   这通常意味着远程客户端会将其中的 <var class="filename">/path/filename.html</var>
   存储到 web 目录树。让 Apache 或者 PHP 自动允许所有人覆盖
   web 目录树下的任何文件显然是很不明智的。因此，要处理类似的请求，必须先告诉
   web 服务器需要用特定的 PHP 脚本来处理该请求。在 Apache 下，可以用
   <em class="emphasis">Script</em> 选项来设置。它可以被放置到
   Apache 配置文件中几乎所有的位置。通常我们把它放置在
   <code class="literal">&lt;Directory&gt;</code> 区域或者 <code class="literal">&lt;VirtualHost&gt;</code> 区域。可以用如下一行来完成该设置：
   <div class="informalexample">
    <div class="example-contents">
<div class="cdata"><pre>
Script PUT /put.php
</pre></div>
    </div>

   </div>
  </p>
  <p class="simpara">
   这将告诉 Apache 将所有对 URI 的 PUT 请求全部发送到  <var class="filename">put.php</var> 脚本，这些
   URI 必须和 PUT 命令中的内容相匹配。当然，这是建立在 PHP 支持
   <var class="filename">.php</var> 扩展名，并且 PHP 已经在运行的假设之上。
   The destination resource for all PUT
   requests to this script has to be the script itself, not a filename the
   uploaded file should have.
  </p>
  <p class="simpara">
   With PHP you would then do something like the following in
   your put.php. This would copy the contents of the uploaded file to the
   file <var class="filename">myputfile.ext</var> on the server.
   You would probably want to perform some checks and/or
   authenticate the user before performing this file copy.
  </p>
  <p class="para">
   <div class="example" id="example-457">
    <p><strong>示例 #1 保存 HTTP PUT 文件</strong></p>
    <div class="example-contents">
     <div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/*&nbsp;PUT&nbsp;data&nbsp;comes&nbsp;in&nbsp;on&nbsp;the&nbsp;stdin&nbsp;stream&nbsp;*/<br /></span><span style="color: #0000BB">$putdata&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"php://input"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"r"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;Open&nbsp;a&nbsp;file&nbsp;for&nbsp;writing&nbsp;*/<br /></span><span style="color: #0000BB">$fp&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"myputfile.ext"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"w"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;Read&nbsp;the&nbsp;data&nbsp;1&nbsp;KB&nbsp;at&nbsp;a&nbsp;time<br />&nbsp;&nbsp;&nbsp;and&nbsp;write&nbsp;to&nbsp;the&nbsp;file&nbsp;*/<br /></span><span style="color: #007700">while&nbsp;(</span><span style="color: #0000BB">$data&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fread</span><span style="color: #007700">(</span><span style="color: #0000BB">$putdata</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1024</span><span style="color: #007700">))<br />&nbsp;&nbsp;</span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$data</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;Close&nbsp;the&nbsp;streams&nbsp;*/<br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$putdata</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div></div></div></body></html>